package com.hkts.reportform.purchase.controller;

import com.hkts.reportform.feign.FeignInterface;
import com.hkts.reportform.purchase.service.PurchaseService;
import com.hkts.reportform.utils.CustomPage;
import com.hkts.reportform.utils.ErpProperties;
import com.hkts.reportform.utils.Result;
import com.hkts.reportform.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * @ClassNamePurchaseController
 * @Description
 * @Author yck
 * @Date2020-11-17 10:31
 * @Version V1.0
 **/
@RestController
@RequestMapping("/erp/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseServiceImp;
    @Autowired
    private FeignInterface feignInterface;
    @Autowired
    private RedisTemplate redisTemplate;

    //存货档案历史删除
    @RequestMapping("/deleteSearchHistory")
    public Result<Object> deleteSearchHistory() {
        Map<String, Object> stringObjectMap = feignInterface.userInfo();
        Map<String, Object> result = (Map<String, Object>) stringObjectMap.get("result");
        redisTemplate.delete(ErpProperties.USER_SEARCH_INFO + result.get("id").toString());
        return ResultUtil.success("删除成功");
    }

    ;

    //存货档案历史
    @RequestMapping("/searchHistory")
    public Result<Object> searchHistory() {
        Map<String, Object> stringObjectMap = feignInterface.userInfo();
        Map<String, Object> result = (Map<String, Object>) stringObjectMap.get("result");
        List<String> id = redisTemplate.opsForList().range(ErpProperties.USER_SEARCH_INFO + result.get("id").toString(), 0, -1);
        return ResultUtil.data(id);
    }

    ;

    //根据编码或名称查存货档案
    @RequestMapping("/selectInventoryByNameOrCode")
    public Result<Object> selectInventoryByNameOrCode(String nameOrCode,CustomPage page) {
        CustomPage customPage = purchaseServiceImp.selectInventoryByNameOrCode(nameOrCode, page);

        return ResultUtil.data(customPage);
    }

    ;

    //根据存货编码查该存货的现存量和采购价格趋势
    @RequestMapping("/selectInventoryDetilByCode")
    public Result<Object> selectInventoryDetilByCode(String code) throws ExecutionException, InterruptedException {
        Map<String, Object> maps = purchaseServiceImp.selectInventoryDetilByCode(code);
        return ResultUtil.data(maps);
    }

    ;

    //用户信息调用
    @RequestMapping("/userInfo")
    public Result<Object> userInfo() throws ExecutionException, InterruptedException {
        Map<String, Object> stringObjectMap = feignInterface.userInfo();
        return ResultUtil.data(stringObjectMap);
    }

    ;

    //根据日期分组聚合订单数量
    @RequestMapping("/getOrderCountByDate")
    public Result<Object> getOrderCountByDate(String dateType, String beginDate, String endDate) throws Exception {
        Map<String, Object> stringObjectMap = purchaseServiceImp.getOrderCountByDate(dateType, beginDate, endDate);
        return ResultUtil.data(stringObjectMap);
    }

    ;
    //Order execution
    @RequestMapping("/orderExecution")
    public Result<Object> orderExecution(String dateType, String beginDate, String endDate, CustomPage page) throws Exception {
        CustomPage stringObjectMap = purchaseServiceImp.orderExecution(dateType, beginDate, endDate, page);
        return ResultUtil.data(stringObjectMap);
    }

    ;
}
